home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / alternatebufferdisplay / alternatebufferdisplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.1 KB  |  357 lines

  1. /*
  2.     File:        AlternateBufferDisplay.c
  3.  
  4.     Contains:    Shows that copybits along with offscreen bitmaps
  5.                 can emulate alternating buffer displays at a reasonable rate.
  6.  
  7.     Written by: John Wang    
  8.  
  9.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/7/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 11/12/91    John Wang        Created
  22.  
  23. */
  24. #include    <QDOffscreen.h>
  25. #include    <Quickdraw.h>
  26. #include    <Menus.h>
  27. #include    <Dialogs.h>
  28. #include    <MacTypes.h>
  29. #include    <TextUtils.h>
  30. #include    <Resources.h>
  31. #include    <Errors.h>
  32.  
  33. #define Gestalttest        0xA1AD
  34. #define NoTrap            0xA89F
  35.  
  36. #define    appleID            128            
  37. #define    appleMenu        0
  38. #define    aboutMeCommand    1
  39.  
  40. #define    fileID            129
  41. #define    quitCommand     1
  42.  
  43. #define    aboutMeDLOG        128
  44. #define    okButton        1
  45.  
  46. #ifndef topLeft
  47. #define topLeft(r)                      (((Point *) &(r))[0])
  48. #endif
  49.  
  50. #ifndef botRight
  51. #define botRight(r)                     (((Point *) &(r))[1])
  52. #endif
  53.  
  54. /*------------------------------------------------------*/
  55. /*    Global Variables.                                    */
  56. /*------------------------------------------------------*/
  57.  
  58. Rect                TotalRect, minRect, WinMinusScroll, InitWindowSize;
  59. WindowPtr            myWindow;
  60. Boolean                DoneFlag;
  61. MenuHandle            mymenu0, mymenu1;
  62. GWorldPtr            offscreenGWorld1, offscreenGWorld2;
  63.  
  64. /*------------------------------------------------------*/
  65. /*    showAboutMeDialog().                                */
  66. /*------------------------------------------------------*/
  67.  
  68. void showAboutMeDialog()
  69. {
  70.     GrafPtr     savePort;
  71.     DialogPtr    theDialog;
  72.     short        itemHit;
  73.  
  74.     GetPort(&savePort);
  75.     theDialog = GetNewDialog(aboutMeDLOG, nil,(GrafPort*)(-1));
  76.     SetPort(theDialog);
  77.  
  78.     do {
  79.         ModalDialog(nil, &itemHit);
  80.     } while (itemHit != okButton);
  81.  
  82.     CloseDialog(theDialog);
  83.  
  84.     SetPort(savePort);
  85.     return;
  86. }
  87.  
  88. /*------------------------------------------------------*/
  89. /*    init().                                                */
  90. /*------------------------------------------------------*/
  91.  
  92. void init()
  93. {
  94.     RgnHandle            tempRgn;
  95.     Rect                BaseRect;
  96.     GDHandle            SaveGD;
  97.     CGrafPtr            SavePort;
  98.     QDErr                 myerr;
  99.     RGBColor            black = {0,0,0}, white = {65535,65535,65535};
  100.  
  101.     InitGraf(&qd.thePort);
  102.     FlushEvents(everyEvent, 0);
  103.     InitWindows();
  104.     InitDialogs(nil);
  105.     InitCursor();
  106.  
  107.     /*    Set up menus.    */
  108.     mymenu0 = GetMenu(appleID);
  109.     AppendResMenu(mymenu0, 'DRVR');
  110.     InsertMenu(mymenu0,0);
  111.     mymenu1 = GetMenu(fileID);
  112.     InsertMenu(mymenu1,0);
  113.     DrawMenuBar();
  114.  
  115.     /* Main initialization.    */
  116.     DoneFlag = false;
  117.  
  118.     /*    Set Rects.    */
  119.     SetRect(&BaseRect, 40, 60, 490, 540);
  120.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-56, 
  121.                 BaseRect.bottom - 76);
  122.     SetRect(&InitWindowSize, WinMinusScroll.left, WinMinusScroll.top, 
  123.                             WinMinusScroll.right, WinMinusScroll.bottom);
  124.     tempRgn =(RgnHandle)GetGrayRgn();
  125.     HLock ((Handle) tempRgn);
  126.     TotalRect = (**tempRgn).rgnBBox;
  127.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  128.                 (**tempRgn).rgnBBox.bottom - 40);
  129.     HUnlock ((Handle) tempRgn);
  130.  
  131.     /*    Open window and set up picture.    */
  132.     GetGWorld (&SavePort, &SaveGD);
  133.     myWindow = NewCWindow(nil, &BaseRect, "\p", true, zoomDocProc, 
  134.                             (WindowPtr) -1, true, 150);
  135.     SetGWorld((CGrafPtr)myWindow, SaveGD);
  136.     DrawGrowIcon (myWindow);
  137.  
  138.     GetGWorld (&SavePort, &SaveGD);
  139.     
  140.     /*    Make first offscreen buffer.    */
  141.     if (myerr=NewGWorld (&offscreenGWorld1, 0, &InitWindowSize, nil, nil, 0))
  142.         Debugger();
  143.     SetGWorld (offscreenGWorld1, nil);
  144.     RGBForeColor(&white);
  145.     PaintRect (&InitWindowSize);
  146.     
  147.     /*    Make second offscreen buffer.    */
  148.     if (NewGWorld (&offscreenGWorld2, 0, &InitWindowSize, nil, nil, 0))
  149.         Debugger();
  150.     SetGWorld (offscreenGWorld2, nil);
  151.     RGBForeColor(&black);
  152.     PaintRect (&InitWindowSize);
  153.     
  154.     SetGWorld (SavePort, SaveGD);
  155. }
  156.  
  157. /*------------------------------------------------------*/
  158. /*    doCommand().                                        */
  159. /*------------------------------------------------------*/
  160.  
  161. void doCommand(mResult)
  162.     long    mResult;
  163. {
  164.     int                     theMenu, theItem;
  165.     char                    daName[256];
  166.     GrafPtr                 savePort;
  167.  
  168.     theItem = LoWord(mResult);
  169.     theMenu = HiWord(mResult);
  170.     
  171.     switch (theMenu) {
  172.         case appleID:
  173.             if (theItem == aboutMeCommand)
  174.                 showAboutMeDialog();
  175.             else {
  176.                 SetMenuItemText(mymenu0, theItem, c2pstr(daName));
  177.                 GetPort(&savePort);
  178.                 (void) OpenDeskAcc(daName);
  179.                 SetPort(savePort);
  180.             }
  181.             break;
  182.  
  183.         case fileID:
  184.             switch (theItem) {
  185.                 case quitCommand:
  186.                     DoneFlag = true;
  187.                     break;
  188.                 default:
  189.                     break;
  190.                 }
  191.             break;
  192.     }
  193.     HiliteMenu(0);
  194.     return;
  195. }
  196.  
  197. /*------------------------------------------------------*/
  198. /*    draw().                                                */
  199. /*------------------------------------------------------*/
  200.  
  201. void draw()
  202. {
  203.     RGBColor        black = {0,0,0}, White = {65535,65535,65535};
  204.     long            delay;
  205.     Str255            theString;
  206.     GDHandle        screensDevice;
  207.     Rect            area;
  208.     int                i;
  209.         
  210.     RGBForeColor (&black);
  211.     RGBBackColor (&White);
  212.     EraseRect(&WinMinusScroll);
  213.     
  214.  
  215.     area = WinMinusScroll;
  216.     
  217.     LocalToGlobal(&topLeft(area));
  218.     LocalToGlobal(&botRight(area));
  219.     
  220.     screensDevice = GetMaxDevice(&area);
  221.     if (screensDevice != nil) {
  222.         (**(**offscreenGWorld1->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  223.         (**(**offscreenGWorld2->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  224.     }
  225.     delay = TickCount();
  226.     for (i=0; i< 10; i++) {
  227.         CopyBits ((BitMap *) *offscreenGWorld2->portPixMap, &myWindow->portBits, 
  228.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  229.         CopyBits ((BitMap *) *offscreenGWorld1->portPixMap, &myWindow->portBits, 
  230.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  231.     }
  232.     delay = TickCount() - delay;
  233.     MoveTo(50,50);
  234.     NumToString(delay, theString);
  235.     DrawString(theString);
  236. }
  237.  
  238. /*------------------------------------------------------*/
  239. /*    main().                                                */
  240. /*------------------------------------------------------*/
  241.  
  242. main()
  243. {
  244.     char            key;
  245.     Boolean            track;
  246.     long            growResult;
  247.     EventRecord     myEvent;
  248.     WindowPtr        whichWindow;
  249.     int                yieldTime;
  250.  
  251.  
  252.     init();
  253.     yieldTime = 0;
  254.     for ( ;; ) {
  255.         if (DoneFlag)
  256.             ExitToShell();
  257.             
  258.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  259.             switch (myEvent.what) {
  260.                 case mouseDown:
  261.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  262.                         case inSysWindow:
  263.                             SystemClick(&myEvent, whichWindow);
  264.                             break;
  265.                         case inMenuBar:
  266.                             doCommand(MenuSelect(myEvent.where));
  267.                             break;
  268.                         case inContent:
  269.                             break;
  270.                         case inDrag:
  271.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  272.                             draw();
  273.                             DrawGrowIcon (whichWindow);
  274.                             break;
  275.                         case inGrow:
  276.                             growResult = GrowWindow (whichWindow, myEvent.where,
  277.                                                     &minRect);
  278.                             SizeWindow(whichWindow, LoWord(growResult), 
  279.                                     HiWord(growResult), true);
  280.                             EraseRect(&whichWindow->portRect);
  281.                             SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  282.                                     whichWindow->portRect.top, 
  283.                                     whichWindow->portRect.right-20, 
  284.                                     whichWindow->portRect.bottom - 20);
  285.                             draw();
  286.                             DrawGrowIcon (whichWindow);
  287.                             break;
  288.                         case inGoAway:
  289.                             track = TrackGoAway (whichWindow, myEvent.where);
  290.                             if (track) {
  291.                                 CloseWindow (whichWindow);
  292.                                 DoneFlag = true;
  293.                                 }
  294.                             break;
  295.                         case inZoomIn:
  296.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  297.                             if (track) {
  298.                                 ZoomWindow (whichWindow, inZoomIn, true);
  299.                                 EraseRect(&whichWindow->portRect);
  300.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  301.                                         whichWindow->portRect.top, 
  302.                                         whichWindow->portRect.right-20, 
  303.                                         whichWindow->portRect.bottom - 20);
  304.                                 draw();
  305.                                 DrawGrowIcon (whichWindow);
  306.                                 }
  307.                             break;
  308.                         case inZoomOut:
  309.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  310.                             if (track) {
  311.                                 ZoomWindow (whichWindow, inZoomOut, true);
  312.                                 EraseRect(&whichWindow->portRect);
  313.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  314.                                         whichWindow->portRect.top, 
  315.                                         whichWindow->portRect.right-20, 
  316.                                         whichWindow->portRect.bottom - 20);
  317.                                 draw();
  318.                                 DrawGrowIcon (whichWindow);
  319.                                 }
  320.                             break;
  321.                         default:
  322.                             break;
  323.                         }
  324.                     break;
  325.                 case keyDown:
  326.                 case autoKey:
  327.                     key = myEvent.message & charCodeMask;
  328.                     if ( myEvent.modifiers & cmdKey )
  329.                         if ( myEvent.what == keyDown )
  330.                             doCommand(MenuKey(key));
  331.                     break;
  332.                 case updateEvt:
  333.                     if ((WindowPtr) myEvent.message == myWindow) {
  334.                         BeginUpdate((WindowPtr) myWindow);
  335.                         EndUpdate((WindowPtr) myWindow);
  336.                         draw();
  337.                         }
  338.                     break;
  339.                 case diskEvt:
  340.                     break;
  341.                 case activateEvt:
  342.                     break;
  343.                 case app4Evt:
  344.                     if ((myEvent.message << 31) == 0) {
  345.                         yieldTime = 30;
  346.                         }
  347.                     else {
  348.                         yieldTime = 0;
  349.                         SetPort((WindowPtr) myWindow);
  350.                         }
  351.                     break; 
  352.                 default:
  353.                     break;
  354.                 }
  355.             }
  356.         }
  357. }